package com.entity;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Zd
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.entity.Zd
 * @author MyEclipse Persistence Tools
 */

public class ZdDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ZdDAO.class);
	// property constants
	public static final String TYPENAME = "typename";
	public static final String PARENTID = "parentid";
	public static final String TYPENAMES = "typenames";
	public static final String TYPECODE = "typecode";

	protected void initDao() {
		// do nothing
	}

	public void save(Zd transientInstance) {
		log.debug("saving Zd instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Zd persistentInstance) {
		log.debug("deleting Zd instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Zd findById(java.lang.Integer id) {
		log.debug("getting Zd instance with id: " + id);
		try {
			Zd instance = (Zd) getHibernateTemplate().get("com.entity.Zd", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Zd instance) {
		log.debug("finding Zd instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Zd instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Zd as model where model." + propertyName
					+ "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTypename(Object typename) {
		return findByProperty(TYPENAME, typename);
	}

	public List findByParentid(Object parentid) {
		return findByProperty(PARENTID, parentid);
	}

	public List findByTypenames(Object typenames) {
		return findByProperty(TYPENAMES, typenames);
	}

	public List findByTypecode(Object typecode) {
		return findByProperty(TYPECODE, typecode);
	}

	public List findAll() {
		log.debug("finding all Zd instances");
		try {
			String queryString = "from Zd";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Zd merge(Zd detachedInstance) {
		log.debug("merging Zd instance");
		try {
			Zd result = (Zd) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Zd instance) {
		log.debug("attaching dirty Zd instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Zd instance) {
		log.debug("attaching clean Zd instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ZdDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ZdDAO) ctx.getBean("ZdDAO");
	}
	/**
	 * 只是得到类型
	 * @return
	 */
	public List gettypenames()
	{
		Session session = this.getSession();
		session.beginTransaction();
		String hql = "select distinct(typenames) from Zd ";
		List list = null;
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}
	/**
	 * 只是得到类型分类
	 * @return
	 */
	public List gettypename()
	{
		Session session = this.getSession();
		session.beginTransaction();
		String hql = "select distinct(typename) from Zd where typename like '%乡' or typename like '%镇%' ";
		List list = null;
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}
	
	public List getzdbm()
	{
		Session session = this.getSession();
		session.beginTransaction();
		String hql = "from Zd where typenames='部门' and typename like '%乡' or typename like '%镇'";
		List list = null;
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			session.close();
		}
		return list;
	}
	public Zd getsql(String sql)
	{
		Session session = this.getSession();
		session.beginTransaction();
		List list = null;
		Zd zd=new Zd();
		try {
			session.beginTransaction().begin();
			Query query = session.createQuery(sql);
			list = query.list();
			zd =(Zd)list.get(0);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			session.close();
		}
		return zd;
	}
}